/**
 * Course form verification: Ensure course information is valid (e.g., date range, time format, non-empty)
 * @param {Object} course - Course info (name, teacher, startDate, endDate, day, startTime, endTime)
 * @returns {Object} Validation result (isValid: Boolean, errors: Error message object)
 */
export const validateCourseForm = (course) => {
  const errors = {};
  const timeRegex = /^([01]\d|2[0-3]):([0-5]\d)$/; 

  // 1. Course Name
  if (!course.name || course.name.trim() === '') {
    errors.name = 'Course name cannot be empty';
  }

  // 2. Teacher Name
  if (!course.teacher || course.teacher.trim() === '') {
    errors.teacher = 'Teacher name cannot be empty';
  }

  // 3. Start Date
  if (!course.startDate) {
    errors.startDate = 'Start date cannot be empty';
  }

  // 4. End Date
  if (!course.endDate) {
    errors.endDate = 'End date cannot be empty';
  }

  // 5. End Date ≥ Start Date
  if (course.startDate && course.endDate) {
    if (new Date(course.endDate) < new Date(course.startDate)) {
      errors.endDate = 'End date cannot be earlier than start date';
    }
  }

  // 6. Day of the Week
  if (!course.day || !['1', '2', '3', '4', '5'].includes(String(course.day))) {
    errors.day = 'Please choose a valid day (Monday to Friday)';
  }

  // 7. Start Time Format
  if (!course.startTime || !timeRegex.test(course.startTime)) {
    errors.startTime = 'Start time format error (required: HH:mm, e.g., 09:00)';
  }

  // 8. End Time Format
  if (!course.endTime || !timeRegex.test(course.endTime)) {
    errors.endTime = 'End time format error (required: HH:mm, e.g., 10:30)';
  }

  // 9. End Time > Start Time
  if (course.startTime && course.endTime && timeRegex.test(course.startTime) && timeRegex.test(course.endTime)) {
    const start = new Date(`2024-01-01 ${course.startTime}`);
    const end = new Date(`2024-01-01 ${course.endTime}`);
    if (end <= start) {
      errors.endTime = 'End time must be later than start time';
    }
  }

  return {
    isValid: Object.keys(errors).length === 0,
    errors
  };
};